package com.ghline.caloncabe.model;

import com.ghline.caloncabe.GameView;
import com.ghline.caloncabe.model.components.Speed;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Fish {	
	
	private static final int BMP_ROWS = 2;
    private static final int BMP_COLUMNS = 2;
    private int x =	0;
    private int y = 0;
    private GameView gameView;
    private Bitmap bmp;
    private Bitmap bmpAlpha;
    private int currentFrame = 0;
    private int width;
    private int height;
    private boolean touched;
	private Speed speed;
	private int direction = 0;
	private Paint paint;

	public Fish(GameView gameView,Bitmap bmp) {
		
		this.gameView = gameView;
        this.bmp = bmp;
        this.width = bmp.getWidth() / BMP_COLUMNS;
        this.height = bmp.getHeight() / BMP_ROWS;
        this.x = gameView.getWidth() / 2 - this.width / 2;
        this.y = gameView.getHeight() / 2 - this.height / 2;
        this.speed = new Speed();
        this.setPaint(new Paint());
	}	

	public void draw(Canvas canvas) {
		updateAnimation();
		int srcX = getDirection() * width;
        int srcY = currentFrame * height;
        Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
        Rect dst = new Rect(x, y, x + width, y + height);
        if(isalpha){
        	canvas.drawBitmap(bmpAlpha, src, dst, paint);
        }else{
        	canvas.drawBitmap(bmp, src, dst, paint);
        }
	}
	
	public void updateAnimation() {
		currentFrame = ++currentFrame % BMP_ROWS;
	}
	
	public boolean collidesWith(Sprite sprite) {
		
		int left1, left2;
		int right1, right2;
		int top1, top2;
		int bottom1, bottom2;
		
		left1 = this.x;
		left2 = sprite.getX();
		right1 = left1 + this.width;
		right2 = left2 + sprite.getWidth();
		top1 = this.y;
		top2 = sprite.getY();
		bottom1 = top1 + this.height;
		bottom2 = top2 + sprite.getHeight();
		
		if (bottom1 < top2)
			return false;
		if (top1 > bottom2)
			return false;
		if (right1 < left2)
			return false;
		if (left1 > right2)
			return false;
		return true;
	}
	
public boolean collidesWith(SuaBien sua) {
		
		int left1, left2;
		int right1, right2;
		int top1, top2;
		int bottom1, bottom2;
		
		left1 = this.x;
		left2 = sua.getX();
		right1 = left1 + this.width;
		right2 = left2 + sua.getWidth();
		top1 = this.y;
		top2 = sua.getY();
		bottom1 = top1 + this.height;
		bottom2 = top2 + sua.getHeight();
		
		if (bottom1 < top2)
			return false;
		if (top1 > bottom2)
			return false;
		if (right1 < left2)
			return false;
		if (left1 > right2)
			return false;
		return true;
	}
	
	public void move(int eventX, int eventY) {
		while (this.getX() <= eventX && this.getY() <= eventY) {
				x += (speed.getXv() * speed.getxDirection()); 
				y += (speed.getYv() * speed.getyDirection());
			break;
		}
		while (this.getX() >= eventX && this.getY() >= eventY) {
				x -= (speed.getXv() * speed.getxDirection()); 
				y -= (speed.getYv() * speed.getyDirection());
			break;
		}
		while (this.getX() >= eventX && this.getY() <= eventY) {
				x -= (speed.getXv() * speed.getxDirection()); 
				y += (speed.getYv() * speed.getyDirection());
			break;
		}
		while (this.getX() <= eventX && this.getY() >= eventY) {
				x += (speed.getXv() * speed.getxDirection()); 
				y -= (speed.getYv() * speed.getyDirection());
			break;
		}
		if (eventX > this.getX()) {
			setDirection(1);
		} else if (eventX < this.getX()) {
			setDirection(0);
		} else {
			setDirection(getDirection());
		}
	}
	
	public void handleActionDown(int eventX, int eventY) {
		
		if (eventX >= (gameView.getWidth() - gameView.getBtPause().getWidth()) 
				&& eventX <= gameView.getWidth() 
				&& eventY >= 0 
				&& y <= gameView.getBtPause().getHeight()) {			
			setTouched(false);
		} else {
			setTouched(true);
		}
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public boolean isTouched() {
		return touched;
	}

	public void setTouched(boolean touched) {
		this.touched = touched;
	}

	public Speed getSpeed() {
		return speed;
	}

	public void setSpeed(Speed speed) {
		this.speed = speed;
	}

	public GameView getGameView() {
		return gameView;
	}

	public void setGameView(GameView gameView) {
		this.gameView = gameView;
	}

	public Bitmap getBmp() {
		return bmp;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setBmp(Bitmap bmp) {
		this.bmp = bmp;
	}
	
	private boolean isalpha = false;
	public void setBmpAlpha(Bitmap bmpAlpha) {
		this.bmpAlpha = bmpAlpha;
		isalpha=true;
	}
	public void setIsAlpha(boolean isalpha){
		this.isalpha=isalpha;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public Paint getPaint() {
		return paint;
	}

	public void setPaint(Paint paint) {
		this.paint = paint;
	}
}
